package org.grayrabbit.cms.content.exp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.grayrabbit.cms.content.base.Common;
import org.grayrabbit.cms.content.base.DocElement;
import org.grayrabbit.cms.entity.Content;
import org.grayrabbit.cms.entity.LinkContent;
import org.grayrabbit.cms.entity.SoftwareContent;
import org.grayrabbit.cms.entity.VideoContent;
import org.grayrabbit.cms.util.DateUtil;
import org.grayrabbit.cms.util.FilePathSptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *		ContentExport.java
 *		@Author      hasau   
 *		@CreateTime  2012-2-8  上午9:38:18	
 **/
public class ContentExport extends Common{
	private static final Logger LOGGER =LoggerFactory.getLogger(ContentExport.class);
	
	/**
	 * @param file 单个的xml文件
	 * @return 成功导出的数量{10,8} 总计10个内容成功导入8个
	 */
	public static boolean singleExport(Content content,String parentName){
		// 单个文件导出 
		writerXml(content,parentName);
		return true;
	}
	
	private static boolean writerXml(Content c,String parentName){
		
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("content")
				.addAttribute(DocElement.CONTEXT_TYPE, c.getContextType());
		
		String tmp ="";		
		addCommonAttr(root, c, tmp); 
		if(DocElement.SOFT.equalsIgnoreCase(c.getContextType())){
			addSoftwareAttr(root, c, tmp); 
		}else if(DocElement.VIDEO.equalsIgnoreCase(c.getContextType()) 
				||DocElement.FLASH.equalsIgnoreCase(c.getContextType())){
			addVideoAttr(root, c, tmp); 
		}else if(DocElement.LINK.equalsIgnoreCase(c.getContextType())){
			addLinkAttr(root, c, tmp);  
		}
		
		return writeContentXML(doc,parentName,c);
		
	}
	 
	/** 写 XML 文件 */
	private static boolean writeContentXML(Document doc, String parentName,Content c) {
		try { 
			 OutputFormat format = OutputFormat.createPrettyPrint(); 
			 format.setEncoding("utf-8");
			 format.setIndent(true); 
			 
			 String path = FilePathSptUtil.UPLOAD_ROOT_PATH+SPT+CONTENT
				+SPT+parentName;
			 File file = new File (path);
			 if(!file.exists()){
				 file.mkdirs();
			 }
			 
			 String fileName = path+SPT +c.getContentId()+POINT+XML; 
			 XMLWriter writer=new XMLWriter(new FileWriter(new File(fileName)),format); 
			 writer.write(doc);
			 writer.close();
			 return true;
		} catch (IOException e) {
			LOGGER.error("xml文件生成异常", e);
			return  false;
		} 	
		
	}

	/**链接文章 属性*/
	private static void addLinkAttr(Element root, Content c, String tmp) {
		LinkContent lc = (LinkContent)c;
		tmp = lc.getLinkUrl()==null?"":lc.getLinkUrl();
		root.addElement(DocElement.LINK_URL).addText(tmp);
	}

	/**视频属性 （包括flash）*/
	private static void addVideoAttr(Element root, Content c, String tmp) {
		VideoContent vc = (VideoContent)c; 
		tmp = vc.getVideoType()==null?"":vc.getVideoType();
		root.addElement(DocElement.VIDEO_TYPE).addText(tmp);
		
		tmp = vc.getVideoTags()==null?"":vc.getVideoTags();
		root.addElement(DocElement.VIDEO_TAGS).addText(tmp);
		
		tmp = vc.getVideoCopyright()==null?"":vc.getVideoCopyright();
		root.addElement(DocElement.VIDEO_COPYRIGHT).addText(tmp);
		
		tmp = vc.getVideoSocer()==null?"":vc.getVideoSocer().toString();
		root.addElement(DocElement.VIDEO_SOCER).addText(tmp); 
		
		tmp = vc.getVideoYear()==null?""
				:DateUtil.formatDateToString(vc.getVideoYear(), "yyyy-MM-dd");
		root.addElement(DocElement.VIDEO_YEAR).addText(tmp);
		
	}

	/**软件属性*/
	private static void addSoftwareAttr(Element root, Content c, String tmp) {
		SoftwareContent sc = (SoftwareContent)c; 
		
		tmp = sc.getSoftwareType()==null?"":sc.getSoftwareType();
		root.addElement(DocElement.SOFTWARE_TYPE).addText(tmp);
		
		tmp = sc.getSoftwareAuth()==null?"":sc.getSoftwareAuth();
		root.addElement(DocElement.SOFTWARE_AUTH).addText(tmp);
		
		tmp = sc.getSoftwareEnv()==null?"":sc.getSoftwareEnv();
		root.addElement(DocElement.SOFTWARE_ENV).addText(tmp);
		
		tmp = sc.getSoftwareLanguage()==null?"":sc.getSoftwareLanguage();
		root.addElement(DocElement.SOFTWARE_LANGUAGE).addText(tmp);
		
		tmp = sc.getSoftwareLevel()==null?"":sc.getSoftwareLevel();
		root.addElement(DocElement.SOFTWARE_LEVEL).addText(tmp);
		
		tmp = sc.getSoftwarePic()==null?"":sc.getSoftwarePic();
		root.addElement(DocElement.SOFTWARE_PIC).addText(tmp);
		
		tmp = sc.getSoftwareScore()==null?"":sc.getSoftwareScore().toString();
		root.addElement(DocElement.SOFTWARE_SCORE).addText(tmp);
	}

	/** 普通文章的属性*/
	private static void addCommonAttr(Element root,Content c,String tmp) {
		root.addElement(DocElement.CONTENT_ID).addText(c.getContentId());
		
		tmp = c.getContentTitle()==null?"":c.getContentTitle();
		root.addElement(DocElement.CONTENT_TITLE).addText(tmp);
		
		tmp = c.getSimpleTitle1()==null?"":c.getSimpleTitle1();
		root.addElement(DocElement.SIMPLE_TITLE_1).addText(tmp);
		
		tmp = c.getSimpleTitle2()==null?"":c.getSimpleTitle2();
		root.addElement(DocElement.SIMPLE_TITLE_2).addText(tmp);
		
		tmp = c.getSimpleTitle3()==null?"":c.getSimpleTitle3();
		root.addElement(DocElement.SIMPLE_TITLE_3).addText(tmp);
		
		tmp = c.getContentAuthor()==null?"":c.getContentAuthor();
		root.addElement(DocElement.CONTENT_AUTHOR).addText(tmp);
		
		tmp = c.getContentSource()==null?"":c.getContentSource();
		root.addElement(DocElement.CONTENT_SOURCE).addText(tmp);
		
		tmp = c.getContextType()==null?"":c.getContextType();
		root.addElement(DocElement.CONTEXT_TYPE).addText(tmp);
		
		tmp = c.getKeywords()==null?"":c.getKeywords();
		root.addElement(DocElement.KEYWORDS).addText(tmp);
		
		tmp = c.getContentContext()==null?"":c.getContentContext();
		root.addElement(DocElement.CONTENT_CONTEXT).addText(tmp);
		
		tmp = c.getContentDescription()==null?"":c.getContentDescription();
		root.addElement(DocElement.CONTENT_DESCRIPTION).addText(tmp);
		
		tmp = c.getCreateTime()==null?"":DateUtil.formatDateToString(c.getCreateTime(),"yyyy-MM-dd HH:mm:ss");
		root.addElement(DocElement.CREATETIME).addText(tmp);
	}

}
